package tjava.apt;

import japa.parser.ParseException;
import japa.parser.ast.CompilationUnit;

import java.io.IOException;
import java.io.PrintWriter;

import tjava.ast.ClassGenerator;

import com.sun.mirror.apt.AnnotationProcessorEnvironment;
import com.sun.mirror.declaration.ClassDeclaration;
import com.sun.mirror.util.SimpleDeclarationVisitor;

class ClassVisitor extends SimpleDeclarationVisitor {

	private AnnotationProcessorEnvironment env;

	public ClassVisitor(final AnnotationProcessorEnvironment env) {
		this.env = env;
	}

	@Override
	public void visitClassDeclaration(final ClassDeclaration classDeclaration) {

		super.visitClassDeclaration(classDeclaration);

		final String className = classDeclaration.getSimpleName();

		ClassGenerator cg = new ClassGenerator(classDeclaration, env);

		CompilationUnit cu;
		try {
			cu = cg.generate();
			PrintWriter writer = env.getFiler().createSourceFile(
					classDeclaration.getPackage() + "." + className + "Gen");
			writer.println(cu.toString());
		} catch (ParseException e1) {
			e1.printStackTrace();
		} catch (IOException e2) {
			e2.printStackTrace();
		}
	}
}